package gui.graphs3d;

import data.CompressionGraphData;
import data.CompressionGraphObject;
import data.ConfigurationGraphData;
import data.GraphData;
import data.MirroringGDWithBS;
import data.MirroringGDWithoutBS;
import java.util.Vector;

import javax.media.j3d.Transform3D;
import javax.vecmath.Vector3f;

import main.ToEngineFacade;

/**
 *
 * @author Elad
 */
public class MirroringGraph extends Graph3D {

	/*private MirroringGDWithBS gDataWith;
    private MirroringGDWithoutBS gDataWithout;*/
	private GraphData gData;
	private boolean withBS;

	public MirroringGraph(GraphData gData) {
		super();
		this.gData = gData;
		if (j3dInstalled){
			addGraphPoints();
			addAxises(new Positive2DAxises());
			setOrbitBehavior();
			add3DText("Buffer Size", 1.11, 0.0, 0.0, 0.05, 0.05, 0.05);
			add3DText("Active Time Units Amount", -0.17, 1.02, 0.0, 0.05, 0.05, 0.05);
			finishGraphCreation();
		}
	}

	/*public MirroringGraph(MirroringGDWithoutBS gData) {
    super();
    gDataWithout = gData;
    withBS = false;
    }*/
	@Override
	protected void addGraphPoints() {
		//      System.out.println("hey " + gData.getClass());
		if (gData instanceof MirroringGDWithBS) {
			MirroringGDWithBS tData = ((MirroringGDWithBS) gData) ;
			int timeUnitsTotalnumber = tData.get_timeUnitsTotalnumber();
			Vector<Integer> activeTimeUnitsID = tData.get_timeUnitsID();
			float y = (float) activeTimeUnitsID.size() / timeUnitsTotalnumber;
			addPoint(0.5f, y, 0f, gData.toString());
			add3DText(tData.get_bufferSize()+"",0.545, -0.02, 0, 0.03, 0.03, 0.03);
			addYTextMarks(timeUnitsTotalnumber);

		} else {            
			Vector<MirroringGDWithBS> allPoints = ((MirroringGDWithoutBS) gData).get_allResults();
			if (allPoints.get(allPoints.size()-1).get_timeUnitsTotalnumber()==-1)
				return; //all the buffers were too small
			addYTextMarks(allPoints.get(allPoints.size()-1).get_timeUnitsTotalnumber());
			addXTextMarks(allPoints);
			for (int i = 0; i < allPoints.size(); i++) {
				int timeUnitsTotalnumber = allPoints.get(i).get_timeUnitsTotalnumber();
				Vector<Integer> activeTimeUnitsID = allPoints.get(i).get_timeUnitsID();                                
				if (activeTimeUnitsID != null) {
					float y = (float) activeTimeUnitsID.size() / timeUnitsTotalnumber;
					float x = ((float) i / allPoints.size()) +0.01f;
					addPoint(x, y, 0f, allPoints.get(i).toString());                    
				}
			}
		}
	}

	private void addXTextMarks(Vector<MirroringGDWithBS> allPoints) {
		double minBS = allPoints.get(0).get_bufferSize();
		double maxBS = allPoints.get(allPoints.size() - 1).get_bufferSize();
		double interval = (maxBS - minBS) / 10.0;
		for (int j = 0; j < 11; j++) {
			add3DText(minBS + j * interval + "", (j * 0.1) + 0.03, -0.03, 0, 0.03, 0.03, 0.03);
		}
	}
	private void addYTextMarks(int timeUnitsTotalnumber) {
		for (int i = 0; i < 11; i++) {
			add3DText((timeUnitsTotalnumber * i) / 10.0 + "", 0.01, (0.1 * i) - 0.01, 0, 0.035, 0.035, 0.035);
		}
	}
	public static void main(String[] args) {

		//       v.add(new ConfigurationGraphObject(1, 2, 3, 0, 0, 0));
		//        v.add(new ConfigurationGraphObject(4, 5, 6, 0, 0, 0));
		//        v.add(new ConfigurationGraphObject(7, 8, 9, 0, 0, 0));
		Vector<Integer> unitId = new Vector<Integer>();
		unitId.add(1);
		unitId.add(2);
		MirroringGDWithBS gd = new MirroringGDWithBS(10, 100, unitId, 1.2, 4);
		//      new MirroringGraph(gd).setVisible(true);
		Vector<Integer> unitId2 = new Vector<Integer>();
		unitId2.add(3);
		unitId2.add(4);
		MirroringGDWithBS gd2 = new MirroringGDWithBS(20, 100, unitId2, 1.2, 2);
		//
		Vector<Integer> unitId3 = new Vector<Integer>();
		unitId3.add(5);
		unitId3.add(6);
		MirroringGDWithBS gd3 = new MirroringGDWithBS(50, 100, unitId3, 1.2, 8);
		//
		Vector<MirroringGDWithBS> v = new Vector<MirroringGDWithBS>();
		v.add(gd);
		v.add(gd2);
		v.add(gd3);
		MirroringGDWithoutBS gdWithout = new MirroringGDWithoutBS(v);
		new MirroringGraph(gdWithout).setVisible(true);
	}

	@Override
	protected void setGraphLocation(Transform3D t) {
		t.setTranslation(new Vector3f(-0.5f, -0.5f, -0.5f));//graph location in window

	}





}
